<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace PcHome\Controller;
use Common\Controller\AdminBaseController;

class AdminTagController extends AdminBaseController
{
    const CNAME = '标签';

    protected $tag_model;

    public function _initialize() {
        parent::_initialize();
        $this->tag_model = D("Tag");
        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 标签列表
     */
    public function index()
    {
        $this->assign('list', $this->tag_model->fetchAll());
        $this->display();
    }

    /**
     * 添加标签
     */
    public function add()
    {
        if (IS_POST) {
            $data = $this->tag_model->create();
            if ($data) {
                $id = $this->tag_model->add($data);
                if ($id) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->tag_model->getError());
            }
        } else {
            $this->display();
        }
    }

    /**
     * 修改标签
     */
    public function edit() {
        if (IS_POST) {
            $data = $this->tag_model->create();
            if ($data) {
                if ($this->tag_model->save($data) !== false) {
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->tag_model->getError());
            }
        } else {
            $tag_id = I('post.tag_id',0,'intval');
            $detail = $this->tag_model->find($tag_id);
            $this->assign('detail', $detail);
            $this->display();
        }
    }

    /**
     * 删除标签
     */
    public function delete() {
        $tag_id = I('post.tag_id',0,'intval');
        if (is_numeric($tag_id)) {
            $this->tag_model->delete($tag_id);
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }
}